package cate.game.role.res.findback;

import cate.common.table.d.GDDailyFb;
import cate.common.table.d.GDFindBack;
import cate.common.table.d.GDFunc;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.res.findback.impl.RoleFindBackDailyFBPO;
import cate.game.role.res.findback.impl.RoleFindBackMainTowerPO;
import cate.game.role.res.findback.impl.RoleFindBackSpiceCallPO;
import cate.game.role.res.findback.msg.FindBackInfoResp;
import cate.game.role.res.findback.po.RoleFindBackFuncPO;
import cate.game.role.res.findback.impl.RoleFindBackQuickFightPO;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass("资源找回")
public class RoleFindBack extends RoleDependent {
    @NoteField("找回功能ID -> 找回数据")
    public Map<Integer, RoleFindBackFuncPO> funcMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (funcMap == null) {
            funcMap = new HashMap<>();
        }
        role.getGame().table.findBack.base.getList().forEach(e -> getFuncPO(e.id));
        funcMap.values().forEach(e -> e.initialize(role));
    }

    @Override
    public void onDaySpan(boolean silence) {
        if (!silence) {
            noticeUpdate();
        }
    }

    /**
     * 同步信息到前端
     */
    public void noticeUpdate() {
        role.sendNow(new FindBackInfoResp(this));
    }

    /**
     * 触发单功能计算找回
     *
     * @param funcId 功能ID
     */
    public void triggerByFuncId(int funcId) {
        RoleFindBackFuncPO funcPO = getFuncPO(funcId);
        if (funcPO == null) {
            return;
        }
        funcPO.trigger();
    }

    private RoleFindBackFuncPO getFuncPO(int funcId) {
        RoleFindBackFuncPO funcPO = funcMap.get(funcId);
        if (funcPO == null) {
            if (funcId == GDFunc.QUICK_FIGHT) {
                funcPO = new RoleFindBackQuickFightPO();
            } else if (funcId == GDFunc.TOWER) {
                funcPO = new RoleFindBackMainTowerPO();
            } else if (funcId == GDFunc.SPICE_CALL) {
                funcPO = new RoleFindBackSpiceCallPO();
            } else if (funcId >= GDFunc.DAILY_FB_FIRST && funcId <= GDFunc.DAILY_FB_END) {
                funcPO = new RoleFindBackDailyFBPO(GDDailyFb.Type.toType(funcId));
            }
            if (funcPO == null) {
                return null;
            }
            funcPO.initialize(role);
            funcMap.put(funcId, funcPO);
        }
        return funcPO;
    }

    /**
     * GM触发单功能计算找回
     *
     * @param funcId 功能ID
     * @param skipDay 是否跨天
     */
    public void GMTriggerByFuncId(int funcId, boolean skipDay) {
        RoleFindBackFuncPO funcPO = getFuncPO(funcId);
        if (funcPO == null) {
            return;
        }
        funcPO.GMTrigger(skipDay);
    }

    public EcResult<Void> fastGetFindBackRes(byte type) {
        EcResult<Void> r = new EcResult<>();
        if (type != GDFindBack.FIND_BACK_TYPE_FREE && type != GDFindBack.FIND_BACK_TYPE_M1) {
            return r.fail("不存在的找回类型");
        }
        MixRes totalCost = new MixRes();
        if (type != GDFindBack.FIND_BACK_TYPE_FREE) {
            for (RoleFindBackFuncPO func : funcMap.values()) {
                MixRes curCost = func.remainTimesCost();
                if (curCost != null) {
                    totalCost.addList(curCost);
                }
            }
            if (totalCost.isEmpty()) {
                return r.fail("没有资源可以找回");
            }
            GameResult<MixResItem> costRes = totalCost.consume(role);
            if (!costRes.ok()) {
                return r.fail(costRes.message);
            }
        }
        MixRes totalReward = new MixRes();
        for (RoleFindBackFuncPO func : funcMap.values()) {
            EcResult<MixRes> findRes = func.onGetAllFindBackRes(type);
            if (r.ok()) {
                totalReward.addList(findRes.data);
            }
        }
        if (totalReward.isEmpty()) {
            return new EcResult<Void>().fail("没有奖励可以找回");
        }
        new MixResAdder()
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .setRes(totalReward)
                .exe(role);
        return r;
    }

    /**
     * 资源找回领奖
     *
     * @param funcId 功能ID
     * @param type 找回类型
     * @return 找回结果
     */
    public EcResult<Void> onGetReward(int funcId, byte type) {
        if (funcId != 0) {
            if (!funcMap.containsKey(funcId)) {
                return new EcResult<Void>().fail("功能不存在");
            }
            return funcMap.get(funcId).onGetFindBackRes(type);
        }
        return fastGetFindBackRes(type);
    }
}
